from fpdf import FPDF
import os
import requests
import time


def download_images_and_create_pdf(image_links, output_pdf):
    # pdf = FPDF()

    headers = {
        'sec-ch-ua-platform': '"Windows"',
        'Referer': 'https://res.cqup.com.cn/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
        'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
        'sec-ch-ua-mobile': '?0',
    }
    for index, link in enumerate(image_links):
        print(link)
        response = requests.get(link, headers=headers)
        image_name = f'E:\日常笔记\ZY\助理笔记图片/{index+1}.jpg'
        with open(image_name, 'wb') as file:
            file.write(response.content)

        time.sleep(0.1)
        # cover = Image.open(image_name)
        # width, height = cover.size
        # if width > 210:
        #     height = height * (210 / width)
        #     width = 210
        # pdf.add_page()
        # pdf.image(image_name, 0, 0, width, height)
        # os.remove(image_name)
    # pdf.output(output_pdf)


def get_image_links():
    image_links = []
    for i in range(1, 476):
        url = f"https://osscdn.cqup.com.cn/files-v2/202204/6254e128ab7815029607f942--ID-LINK--6254e1287c8e0--SPLITE-SAVED-UNIQUEID--1649729832/{i}.jpg"
        image_links.append(url)

    return image_links


if __name__ == "__main__":
    # 替换为你要下载的 PDF 的链接和保存的路径及文件名
    pdf_url = "https://osscdn.cqup.com.cn/files-v2/202204/6254e128ab7815029607f942--ID-LINK--6254e1287c8e0--SPLITE-SAVED-UNIQUEID--1649729832/8.jpg"
    save_path = "《中医执业（助理）医师考试笔记》-郭海山.pdf"
    image_links = get_image_links()
    print(image_links)
    download_images_and_create_pdf(image_links, save_path)